﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common
{
    /// <summary>
    /// DataTable转换
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataTableToModel<T> where T : new()
    {
        /// <summary>
        /// 将DataSet轉化為List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static List<T> ConvertToList(DataSet ds)
        {
            // 定義集合
            List<T> ts = new List<T>();
            try
            {
                // 獲得此模型的類型
                Type type = typeof(T);
                string tempName = "";
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
                        // 獲得此模型的公用變量
                        PropertyInfo[] propertys = t.GetType().GetProperties();

                        foreach (PropertyInfo proper in propertys)
                        {
                            tempName = proper.Name.ToUpper();

                            // 檢查DataTable是否包含此列
                            if (dt.Columns.Contains(tempName))
                            {
                                object value = dr[tempName];
                                if (value != DBNull.Value)
                                {
                                    if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                        proper.SetValue(t, value, null);
                                    else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                        proper.SetValue(t, value, null);
                                    else
                                    {
                                        if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                        {
                                            string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                            propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                            proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                        }
                                        else
                                            proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                                    }
                                }
                            }
                        }
                        ts.Add(t);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ts;
        }


        public static List<T> ConvertToList(DataTable dt)
        {
            // 定義集合
            List<T> ts = new List<T>();
            try
            {
                // 獲得此模型的類型
                Type type = typeof(T);
                string tempName = "";
                foreach (DataRow dr in dt.Rows)
                {
                    T t = new T();
                    // 獲得此模型的公用變量
                    PropertyInfo[] propertys = t.GetType().GetProperties();
                    foreach (PropertyInfo proper in propertys)
                    {
                        tempName = proper.Name.ToUpper();
                        // 檢查DataTable是否包含此列
                        if (dt.Columns.Contains(tempName))
                        {
                            object value = dr[tempName];
                            if (value != DBNull.Value)
                            {
                                if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                    proper.SetValue(t, value, null);
                                else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                    proper.SetValue(t, value, null);
                                else
                                {
                                    if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                    {
                                        string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                        propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                        proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                    }
                                    else
                                        proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                                }
                            }
                        }
                    }
                    ts.Add(t);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ts;
        }
    }
}
